<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
	
	<link rel="stylesheet" href="styles.css">

	<script type="importmap">
	  {
		"imports": {
		  "three": "https://cdn.jsdelivr.net/npm/three@0.164.0/build/three.module.js",
		  "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.164.0/examples/jsm/",
		  "pet/": "https://cdn.jsdelivr.net/npm/pet-gen@1.7.1/src/"
		}
	  }
	</script>
</head>

<body>
	<div class="header white">
		<h1 class="white">Deferred generation</h1>
		<h3 class="white">Equirectangular texture generated during rendering by 
		<a href="https://boytchev.github.io/texture-generator/docs/camouflage.html">camouflage.js</a></h3>
		<progress id="progressBar" value="0" max="100"> 0% </progress>
	</div>
	
	<div class="footnote white">
		<a href="#" onclick="window.history.back(); return false;">Back</a>
	</div>
	
	
	<script type="module">

		import * as THREE from "three";
		import { OrbitControls } from "three/addons/controls/OrbitControls.js";
		import * as PET from "pet/patterns/camouflage.js";




		// setting up the scene

		var scene = new THREE.Scene();
		scene.background = new THREE.Color( 'black' );

		var camera = new THREE.PerspectiveCamera( 40, innerWidth/innerHeight );
		camera.position.set( 0, 0, 13 );
		camera.lookAt( scene.position );

		var renderer = new THREE.WebGLRenderer( { antialias: true } );
		renderer.setSize( innerWidth, innerHeight );
		renderer.setAnimationLoop( animationLoop );
		document.body.appendChild( renderer.domElement );

		window.addEventListener( "resize", ( /*event*/ ) => {

			camera.aspect = innerWidth/innerHeight;
			camera.updateProjectionMatrix( );
			renderer.setSize( innerWidth, innerHeight );

		} );

		var hiddenCamera = camera.clone();
		hiddenCamera.position.set( 0, 0, 1 );

		var controls = new OrbitControls( hiddenCamera, renderer.domElement );
		controls.enableDamping = true;
		controls.autoRotate = true;
		controls.autoRotateSpeed = 0.5;

		var light = new THREE.DirectionalLight( 'white', 6 );
		light.decay = 0;
		light.position.set( 0, 0, 10 );
		scene.add( light );


		// setiing up the ball with the dererred texture

		var ball = new THREE.Mesh(
			new THREE.SphereGeometry( 3 ),
			PET.material( new THREE.MeshLambertMaterial( {
				map: PET.texture( PET.pattern, true, { width: 2048, height: 1024 } ),
				side: THREE.DoubleSide,
				transparent: true,
			} ) )
		);

		scene.add( ball );


		var progressBar = document.getElementById( 'progressBar' );

		function animationLoop( /*t*/ ) {

			progressBar.value = 100*ball.material.map.update( 10 );

			if ( progressBar.value < 100 )
				ball.material.map.needsUpdate = true;
			else
				progressBar.style.display = 'none';

			controls.update( );

			ball.scale.setScalar( 1/controls.getDistance() );
			ball.quaternion.copy( hiddenCamera.quaternion ).conjugate();

			renderer.render( scene, camera );

		}

	</script>
</body>
</html>